package cn.edu.nankai.bookstore.dao.impi;

import cn.edu.nankai.bookstore.dao.ItemDao;
import cn.edu.nankai.bookstore.domain.po.Item;


import java.util.ArrayList;
import java.util.List;

public class ItemDaoImpl extends GenericBaseDao implements ItemDao {

    @Override
    public List<Item> findAllByOrderId(int id) {
        String sql = "select * from order_book where order_id = ?";
        List<Item> items = new ArrayList<>();
        try {
            this.getConnection();
            this.executeQuery(sql, id);
            while (rs.next()) {
                items.add(new Item(
                        rs.getInt("order_id"),
                        rs.getInt("book_id"),
                        rs.getInt("amount"),
                        rs.getDouble("discount"),
                        rs.getDouble("total"),
                        rs.getDouble("total_discount")
                ));
            }
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }


    @Override
    public Item findById(Integer orderId) {
        //item table have no pk
        return null;
    }

    @Override
    public List<Item> findAll() {
        String sql = "select * from order_book";
        ArrayList<Item> items = new ArrayList<>();

        try {
            this.getConnection();
            this.executeQuery(sql);
            while (rs.next()) {
                items.add(new Item(
                        rs.getInt("order_id"),
                        rs.getInt("book_id"),
                        rs.getInt("amount"),
                        rs.getDouble("discount"),
                        rs.getDouble("total"),
                        rs.getDouble("total_discount")
                ));
            }
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }

    @Override
    public int insert(Item item) {
        String sql = "insert into order_book values(?,?,?,?,?,?)";
        int res = 0;
        try {
            this.getConnection();
            this.executeInsert(sql, item.getOrderId(),
                    item.getBookId(),
                    item.getAmount(),
                    item.getDiscount(),
                    item.getTotal(),
                    item.getDiscountTotal());
            res = result;
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int update(Item item) {
        String sql = "update order_book set amount = ?,discount = ?,total = ?,discount_total = ?" +
                "where order_id = ? and book_id = ?";
        int res = 0;
        try {
            this.getConnection();
            this.executeUpdate(sql, item.getAmount(),
                    item.getDiscount(),
                    item.getTotal(),
                    item.getDiscountTotal(),
                    item.getOrderId(),
                    item.getBookId());
            res = result;
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Integer orderId) {
        //item table have no pk
        return 0;
    }

    @Override
    public int delete(Item item) {
        String sql = "delete from order_book where order_id = ? and book_id = ?";
        int res = 0;
        try {
            this.getConnection();
            this.executeUpdate(sql, item.getOrderId(), item.getBookId());
            res = result;
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public List<Item> findBySql(String sql, Object... params) {
        List<Item> items = null;
        try {
            this.getConnection();
            this.executeQuery(sql, params);
            if (rs != null) {
                items = new ArrayList<>();
                while (rs.next()) {
                    Item item = new Item(
                            rs.getInt("order_id"),
                            rs.getInt("book_id"),
                            rs.getInt("amount"),
                            rs.getDouble("discount"),
                            rs.getDouble("total"),
                            rs.getDouble("total_discount")
                    );
                    items.add(item);
                }
            }
            this.closeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }

}
